package com.tianma315.web.small_code.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;

import com.tianma315.core.common.utils.Result;
import com.tianma315.core.small_code.domain.SmallRecordDO;
import com.tianma315.core.small_code.service.SmallRecordService;
import com.tianma315.core.small_code.vo.SmallRecordListVO;
import com.tianma315.core.small_code.vo.SmallRecordVO;
import com.tianma315.web.common.base.BaseController;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

/**
 * 
 * <pre>
 * 外包装管理记录
 * </pre>
 * <small> 2019-08-10 10:06:48 | Wen</small>
 */
@Controller
@RequestMapping("/small_code/smallRecord")
public class SmallRecordController extends BaseController {
	@Autowired
	private SmallRecordService smallRecordService;
	
	@GetMapping()
	@RequiresPermissions("small_code:smallRecord:smallRecord")
	String SmallRecord(){
	    return "small_code/smallRecord/smallRecord";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("small_code:smallRecord:smallRecord")
	public Result<Page<SmallRecordDO>> list(Integer pageNumber, Integer pageSize, SmallRecordDO smallRecordDTO){
		// 查询列表数据
        Page<SmallRecordDO> page = new Page<>(pageNumber, pageSize);
        
        Wrapper<SmallRecordDO> wrapper = new EntityWrapper<SmallRecordDO>();
//        wrapper.eq("merchant_id",getMerchantId());
        wrapper.like("small_code",smallRecordDTO.getSmallCode());
        page = smallRecordService.selectPage(page, wrapper);
        int total = smallRecordService.selectCount(wrapper);
        page.setTotal(total);
        return Result.ok(page);
	}
	
	@GetMapping("/add")
	@RequiresPermissions("small_code:smallRecord:add")
	String add(){
	    return "small_code/smallRecord/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("small_code:smallRecord:edit")
	String edit(@PathVariable("id") Integer id, Model model){
		SmallRecordDO smallRecord = smallRecordService.selectById(id);
		model.addAttribute("smallRecord", smallRecord);
	    return "small_code/smallRecord/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("small_code:smallRecord:add")
	public Result<String> save( SmallRecordDO smallRecord){
		if(smallRecordService.insert(smallRecord)){
			return Result.ok();
		}
		return Result.fail();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("small_code:smallRecord:edit")
	public Result<String>  update( SmallRecordDO smallRecord){
		smallRecordService.updateById(smallRecord);
		return Result.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("small_code:smallRecord:remove")
	public Result<String>  remove( Integer id){
		if(smallRecordService.deleteById(id)){
            return Result.ok();
		}
		return Result.fail();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("small_code:smallRecord:batchRemove")
	public Result<String>  remove(@RequestParam("ids[]") Integer[] ids){
		smallRecordService.deleteBatchIds(Arrays.asList(ids));
		return Result.ok();
	}

	@GetMapping("/product")
//	@RequiresPermissions("small_code:smallRecord:smallRecord")
	String smallBox(){
		return "small_code/smallRecord/product";
	}

	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/saveRecord")
	public Result<String> saveRecord(@RequestBody SmallRecordVO smallRecord){
//		smallRecord.setMerchantId(getMerchantId());
		smallRecord.setCreateBy(getUserId());
		if(smallRecordService.addSmallRecord(smallRecord)){
			return Result.ok();
		}
		return Result.fail();
	}

	@GetMapping("/getSmallRecordListVOByCode")
	@ResponseBody
	public Result<SmallRecordListVO> getSmallRecordListVOByCode(String code){
		SmallRecordListVO smallRecordListVO = smallRecordService.getSmallRecordListVOByCode(code);
		return Result.ok(smallRecordListVO);
	}

	@GetMapping("/getSmallRecordListVOBySmallCode")
	@ResponseBody
	public Result<SmallRecordListVO> getSmallRecordListVOBySmallCode(String smallCode){
		SmallRecordListVO smallRecordListVO = smallRecordService.getSmallRecordListVOBySmallCode(smallCode);
		return Result.ok(smallRecordListVO);
	}

//	/**
//	 * 保存
//	 */
//	@ResponseBody
//	@PostMapping("/saveRecordAndProduct")
//	public Result<String> saveRecordAndProduct(@RequestBody SmallRecordVO smallRecord){
////		smallRecord.setCompanyId(getCompanyId());
//		smallRecord.setCreateBy(getUserId());
//		if(smallRecordService.addSmallRecordAndStock(smallRecord)){
//			return Result.ok();
//		}
//		return Result.fail();
//	}
	
}
